<template>
	<div>
		<el-tabs v-model="activeName" @tab-click="handleTabChange">
			<el-tab-pane v-for="(item, index) in tabsList" :key="index" :label="item.label" :name="item.value"> </el-tab-pane>

			<div class="content-card">
				<html-label :title="activeLabelTitle">
					<div v-if="!(nowRowData.dataStatus && nowRowData.dataStatus !== '0') && ['BaseDetail', 'Extend', 'ObjectOther'].includes(activeName)">
						<el-button  v-auth="[nowRowData.isOperate==1,baseConfig.auth.edit]" v-if="activeName == 'ObjectOther'" type="primary" @click="handleOperate('edit')"> 信息编辑 </el-button>
						<el-button  v-auth="[nowRowData.isOperate==1,baseConfig.auth.edit]" v-else type="primary" plain @click="handleOperate('rechange')"> 信息变更 </el-button>
					</div>
				</html-label>

				<component class="pd10" :is="whickComm[activeName]" :soleKey="`dpServiceOrgHome-${nowRowData.creditCode}-${nowRowData.orgHomeName}`" @refresh="handleRefresh" :nowRowData="nowRowData"></component>
			</div>
		</el-tabs>
	</div>
</template>

<script setup lang="ts">
import BaseDetail from './BaseDetail/index.vue'
import Extend from './Extend/index.vue'
import OperateRecord from './OperateRecord/index.vue'
import practitioner from './practitioner/index.vue'
import ServiceTickets from './ServiceTickets/index.vue'
import DataChangeLog from '@/views/datacenter/components/DataChangeLog/index.vue'
import { baseConfig } from '../../config'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
import { useCommonStore } from '@/stores/common'
import bus from '@/utils/eventbus'
import ChainBlockInfo from './ChainBlockInfo/index.vue'

const { onDelete, onMessageChange } = useOperate(4)

const { setTempParams } = useCommonStore()
const tabsList = [
	{ label: '基础信息', value: 'BaseDetail' },
	{ label: '扩展数据', value: 'Extend' },
	{ label: '运营记录', value: 'OperateRecord' },
	{ label: '从业人员', value: 'practitioner' },
	{ label: '社区居家养老服务工单', value: 'ServiceTickets' },
	{ label: '信息编辑记录', value: 'DataChangeLog' },
	{ label: '上链信息', value: 'ChainBlockInfo' },
]

const whickComm = {
	BaseDetail,
	Extend,
	OperateRecord,
	practitioner,
	ServiceTickets,
  	DataChangeLog,
	ChainBlockInfo
}
const activeName = ref('BaseDetail')
type Props = {
	nowRowData?: any
}

const props = withDefaults(defineProps<Props>(), {})
provide('nowRowData', props.nowRowData)

const emit = defineEmits(['refresh', 'rechange'])
const activeLabelTitle = ref('基本信息')
const openEdit = () => {
	onMessageChange().then(() => {
		const activeStr = {
			BaseDetail: 1,
			Extend: 2,
		}
		setTempParams({
			active: activeStr[activeName.value] || 1,
		})
		emit('rechange', 'Rechange', props.nowRowData, '信息变更数据')
	})
}

const handleOperate = (type) => {
	if (type == 'delete') {
		onDelete(props.nowRowData[baseConfig.rowKey], baseConfig.api.delete, props.nowRowData.orgHomeName)
			.then(() => {
				emit('refresh')
			})
			.catch(() => {
				return
			})
		return
	} else if (type == 'rechange') {
		openEdit()
	} else {
		emit('rechange', 'edit', props.nowRowData, '信息编辑数据')
	}
}

bus.on('menu-operate', (res) => {
	if (res.pageName == baseConfig.pageName) {
		handleOperate(res.operate)
	}
})

const handleTabChange = (tab) => {
	activeLabelTitle.value = tab.props.label
}

const handleRefresh = () => {
	emit('refresh')
}
onMounted(() => {})
</script>

<style scoped></style>
